"""
管理后台 - 常见问题管理接口
"""
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.ext.asyncio import AsyncSession

from app.dependencies import get_db, get_current_user
from app.schemas.common import ResponseModel, PaginatedResponse
from app.schemas.content import FAQCreate, FAQUpdate, FAQResponse
from app.crud.base import CRUDBase
from app.models.content import FAQ


router = APIRouter()
crud_faq = CRUDBase[FAQ, FAQCreate, FAQUpdate](FAQ)


@router.get("", response_model=ResponseModel)
async def get_faqs(
    page: int = Query(1, ge=1),
    page_size: int = Query(10, ge=1, le=100),
    category: Optional[str] = Query(None),
    status: Optional[int] = Query(None),
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取常见问题列表
    """
    filters = {}
    if category:
        filters["category"] = category
    if status is not None:
        filters["status"] = status
    
    skip = (page - 1) * page_size
    faqs = await crud_faq.get_multi(db, skip=skip, limit=page_size, filters=filters)
    total = await crud_faq.get_count(db, filters=filters)
    
    return ResponseModel(
        data=PaginatedResponse(
            total=total,
            page=page,
            page_size=page_size,
            items=[FAQResponse.model_validate(f) for f in faqs]
        )
    )


@router.get("/{faq_id}", response_model=ResponseModel)
async def get_faq(
    faq_id: int,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取常见问题详情
    """
    faq = await crud_faq.get_by_id(db, id=faq_id)
    if not faq:
        raise HTTPException(status_code=404, detail="问题不存在")
    
    return ResponseModel(data=FAQResponse.model_validate(faq))


@router.post("", response_model=ResponseModel)
async def create_faq(
    faq_in: FAQCreate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    创建常见问题
    """
    faq = await crud_faq.create(db, obj_in=faq_in)
    return ResponseModel(
        code=201,
        message="创建成功",
        data=FAQResponse.model_validate(faq)
    )


@router.put("/{faq_id}", response_model=ResponseModel)
async def update_faq(
    faq_id: int,
    faq_in: FAQUpdate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    更新常见问题
    """
    faq = await crud_faq.get_by_id(db, id=faq_id)
    if not faq:
        raise HTTPException(status_code=404, detail="问题不存在")
    
    faq = await crud_faq.update(db, db_obj=faq, obj_in=faq_in)
    return ResponseModel(
        message="更新成功",
        data=FAQResponse.model_validate(faq)
    )


@router.delete("/{faq_id}", response_model=ResponseModel)
async def delete_faq(
    faq_id: int,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    删除常见问题
    """
    success = await crud_faq.delete(db, id=faq_id)
    if not success:
        raise HTTPException(status_code=404, detail="问题不存在")
    
    return ResponseModel(message="删除成功")

